package com.sihan.framework.etm.service.impl;

import com.sihan.framework.etm.common.PageRequest;
import com.framework.model.PageResult;
import com.framework.mybatis.Condition;
import com.framework.mybatis.service.impl.BaseServiceImpl;
import com.framework.util.PageUtils;
import com.github.pagehelper.PageHelper;
import com.sihan.framework.etm.dto.LogLookDTO;
import com.sihan.framework.etm.entity.Log;
import com.sihan.framework.etm.entity.LogBack;
import com.sihan.framework.etm.mapper.LogMapper;
import com.sihan.framework.etm.service.LogBackService;
import com.sihan.framework.etm.service.LogService;

import java.util.*;

import com.sihan.framework.etm.vo.LogLookVO;
import com.sihan.framework.etm.vo.LogLookValueVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class LogServiceImpl extends BaseServiceImpl<Log, Long> implements LogService {

  @Autowired
  private LogMapper logMapper;
  @Autowired
  private LogBackService logBackService;

  @Override
  public PageResult<Log> selectPage(PageRequest pageRequest,Date startTime,Date endTime, String ip, String operation, Integer type) {
    PageHelper.startPage(pageRequest);
    List<Log> result = logMapper.selectPage(startTime, endTime, ip, operation, type);
    return PageUtils.toPageResult(result);
  }

  @Override
  public void deleteLogByIds(List<Long> logIds) {
    for (Long logId : logIds) {
      Log log = new Log();
      log.setId(logId);
      log.setIsDelete(true);
      logMapper.updateById(log);
    }
  }

  @SuppressWarnings("unchecked")
  @Override
  public List<Log> findLogAllByType(Integer logType) {
    if(logType == null) {
      return null;
    }
    return logMapper.selectList(new Condition<>(new Log(logType)).order("create_time", "desc"));
  }

  @Override
  public List<String> findOperas(Integer logType) {
    return logMapper.findOperas(logType);
  }

  @Override
  public List<String> findOperaUsers(Integer logType) {
    return logMapper.findOperaUsers(logType);
  }

  @Override
  public LogLookVO look(LogLookDTO dto) {
    LogLookVO logLook = new LogLookVO();
    Map<String, Object> param = new HashMap<>();
    param.put("createUser", dto.getCreateUser());
    param.put("startTime", dto.getStartTime());
    param.put("endTime", dto.getEndTime());
    param.put("logType", dto.getLogType());
    List<Map<String, Object>> list = logMapper.look(param);
    if(list != null && list.size() > 0) {
      List<String> legendData = new ArrayList<>();
      Map<String, Boolean> selected = new HashMap<>();
      List<LogLookValueVO> seriesData = new ArrayList<>();
      for (int i = 0; i < list.size(); i++) {
        String key = list.get(i).get("operation").toString();
        long value = Long.parseLong(list.get(i).get("mun").toString());
        legendData.add(key);
        if(i < 5) {
          selected.put(key, true);
        } else {
          selected.put(key, false);
        }
        seriesData.add(new LogLookValueVO(key, value));
      }

      logLook.setLegendData(legendData);
      logLook.setSelected(selected);
      logLook.setSeriesData(seriesData);

    }
    return logLook;
  }

  @Override
  public LogLookVO look1(LogLookDTO dto) {
    LogLookVO logLook = new LogLookVO();
    Map<String, Object> param = new HashMap<>();
    param.put("operation", dto.getOperation());
    param.put("startTime", dto.getStartTime());
    param.put("endTime", dto.getEndTime());
    param.put("logType", dto.getLogType());
    List<Map<String, Object>> list = logMapper.look1(param);
    if(list != null && list.size() > 0) {
      List<String> legendData = new ArrayList<>();
      Map<String, Boolean> selected = new HashMap<>();
      List<LogLookValueVO> seriesData = new ArrayList<>();
      for (int i = 0; i < list.size(); i++) {
        String key = list.get(i).get("create_user").toString();
        long value = Long.parseLong(list.get(i).get("mun").toString());
        legendData.add(key);
        if(i < 5) {
          selected.put(key, true);
        } else {
          selected.put(key, false);
        }
        seriesData.add(new LogLookValueVO(key, value));
      }

      logLook.setLegendData(legendData);
      logLook.setSelected(selected);
      logLook.setSeriesData(seriesData);

    }
    return logLook;
  }

  @Transactional
  @Override
  public void autoBackUp() {
    //获取6月前的日志
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.MONTH, -6);
    List<Log> beforeSixLogs = logMapper.findBeforeSixLogs(calendar.getTime());

    if(beforeSixLogs != null && beforeSixLogs.size() > 0) {
      List<LogBack> list = new ArrayList<>();

      for (Log l : beforeSixLogs) {
        LogBack lrb = new LogBack();
        BeanUtils.copyProperties(l, lrb);
        list.add(lrb);
      }

      logBackService.batchInsert(list);
      logMapper.deleteBeforeSixLogs(calendar.getTime());
    }

  }
}
